﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.XtraBars;
using DevExpress.XtraTab;
using System.Linq;

namespace QuanLyPhongKham
{
    public partial class MainForm : BaseRibbonForm
    {
        
        #region fields & properties

        private DAL.AccessData m;
        private string sql = "", user = "", s_mabn = "", s_mmyy = "", s_makp_loc = "", s_menu = "", s_makp = "", s_ngaykham = "";
        private bool bTiepdon = false, bEdit = false;
        private long l_maql = 0;
        private decimal d_dongia = 0, d_tienthuoc = 0;
        private int i_userid = 0, i_hen = 0;
        private DataSet ds = new DataSet();
        private DataSet dsxml = new DataSet();
        private DataTable dtpk = new DataTable();
        private DataTable dtgiavp = new DataTable();
        private DataTable dtviettat = new DataTable();
        private DataTable dtbs = new DataTable();
        
        #endregion


        public MainForm()
        {
            InitializeComponent();
            UserLookAndFeel.Default.SetSkinStyle("Office 2010 Blue");
           
        }

        private void LoadFirstScreen() {
            this.tabMain.TabPages.Clear();
            var firstForm = new ucFirst();
            firstForm.tileKhamBenh.ItemClick += new DevExpress.XtraEditors.TileItemClickEventHandler(tileKhamBenh_ItemClick);
            OpenTab("Welcome", firstForm);
        }

        void tileKhamBenh_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            OpenTab("Khám Bệnh", new ucMain());
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            frmLogin f = new frmLogin();
            f.ShowDialog(this);
            this.FormLogin = f;
            this.LoadFirstScreen();
            //temp register events================================
            

            //====================================================

            //if (f.i_userid != 0)
            //{
            //    m = f.m;
            //    user = m.user;
            //    if (m.get_data("select * from " + user + ".dmgiavp where id=-9999").Tables[0].Rows.Count == 0)
            //    {
            //        m.execute_data("insert into " + user + ".dmgiavp(id,ten,dongia,form,readonly,pttt) values(-9999,'Tiền khám bệnh',0,'',1,0)");
            //    }
            //    if (m.get_data("select * from " + user + ".dmgiavp where id=-9998").Tables[0].Rows.Count == 0)
            //    {
            //        m.execute_data("insert into " + user + ".dmgiavp(id,ten,dongia,form,readonly,pttt) values(-9998,'Tiền thuốc',0,'',1,0)");
            //    }
            //    s_makp_loc = f.s_makp.Trim(',').Replace(",", "','");
            //    s_makp = f.s_makp.Trim(',');
            //    s_menu = f.s_menu;
            //    i_userid = f.i_userid;
            //    this.Text = "NIKSOFT - Quản Lý Phòng Khám <" + f.s_hoten + ">";
            //    bTiepdon = s_menu.IndexOf("c01") != -1;
            //    //listDsbn.Visible = true;
            //    //if (bTiepdon)
            //    //{
            //    //    gCaclankham.Text = "Các lần đăng ký khám";
            //    //    gDausinhton.Visible = false;
            //    //    gCaclankham.Location = new Point(2, 2);
            //    //    pTiepdon.Visible = true;
            //    //    pDscho.Visible = false;
            //    //    tVienphi.Visible = true; tTienich.Visible = false;
            //    //    tDonthuoc.Visible = false; tPttt.Visible = false;
            //    //}
            //    //else
            //    //{
            //    //    gCaclankham.Text = "Các lần khám bệnh";
            //    //    pTiepdon.Visible = false; tTienich.Visible = false;
            //    //    gDausinhton.Visible = true;
            //    //    gKhambenh.Text = "Thông tin khám bệnh";
            //    //    lChon.Text = "      THÔNG TIN KHÁM BỆNH";
            //    //    butDiungthuoc.BackColor = Color.MintCream;
            //    //    butDiungthuoc.ForeColor = Color.Black;
            //    //}
            //    //if (f.bAdmin)
            //    //{
            //    //    tTienich.Visible = true;
            //    //    tVienphi.Visible = true;
            //    //}
            //    //s_mmyy = ngaylamviec.Text.Substring(3, 2) + ngaylamviec.Text.Substring(8, 2);
            //    //if (!m.bMmyy(s_mmyy))
            //    //{
            //    //    m.tao_table(s_mmyy);
            //    //    m.tao_function(s_mmyy);
            //    //    m.upd_table(s_mmyy, i_userid);
            //    //}
            //    //load_tieude();
            //    //loaddanhmuc();
            //    //load_loaikham();
            //    //load_viettat();
            //    //if (!bTiepdon)
            //    //{
            //    //    pGiakham.Visible = true;
            //    //    load_formnhap();
            //    //}
            //    //if (bTiepdon)
            //    //{
            //    //    pKhamvu.Visible = false; pKhampk.Visible = false; pGiakham.Visible = false; pTheodoitimthai.Visible = false; pSieuammau.Visible = false;
            //    //    butIn.Visible = false; pKhamthai.Visible = false; pSieuam.Visible = false; pCanlamsang.Visible = false; pSieuam4D_12tuan.Visible = false; pSieuam_thai3thang.Visible = false;
            //    //    lViettat.Visible = false; pSieuam_thainghen.Visible = false; pHiemmuon.Visible = false; pSieuam_pk.Visible = false; pEmpty.Visible = false;
            //    //}
            //    //else if (!bTiepdon) load_vv_mabn_chuakham();
            //    //if (pDscho.Visible) pDscho.Focus();
            //    //else mabn1.Focus();
            //    //ngaykham.Text = m.ngayhienhanh_server;
            //    //giokham.Text = m.giohienhanh_server;
            //    //empty_text();
            //    //cboGioitinh.SelectedIndex = 1;
            //    //cbonn.SelectedIndex = 1;
            //    //ena_doituong(false);
            //    //ena_nuoc(false);
            //}
            //else Application.Exit();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab("Khám Bệnh", new ucMain());
        }

       

        private void btnDMBacSy_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab("DM Bác Sỹ", new ucDmBacSy(this.FormLogin.m));
        }

        private void btnDMPhongKham_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab("DM Phòng Khám", new ucDmPhongKham(this.FormLogin.m));
        }

        private void btnDMDoiTuong_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab("DM Đối Tượng", new ucDmDoiTuong(this.FormLogin.m));
        }

        private void btnDMGiaVienPhi_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab("Giá Viện Phí", new ucGiaKham2(this.FormLogin.m));
        }

        private void barButtonItem15_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab("Phân quyền", new ucPhanQuyenSuDung(this.FormLogin.m));
        }
        


        private void tabMain_CloseButtonClick(object sender, EventArgs e)
        {
            var currentTab = this.tabMain.SelectedTabPage;
            if (currentTab == null)
                return;
            var ctrl = currentTab.Controls.Count > 0 ? currentTab.Controls[0] : null;
            if (ctrl != null) {
                if (ctrl is BaseUserControl) {
                    string result = (ctrl as BaseUserControl).Validate();
                    if (result == string.Empty)
                    {
                        ctrl.Dispose();
                        this.tabMain.TabPages.Remove(currentTab);
                    }
                    else {
                        MessageBox.Show(result);
                        
                    }
                }
            }
        }



        #region ================ Common methods ======================

        private XtraTabPage MakeNewTab(string name)
        {
            XtraTabPage t = new XtraTabPage();
            t.Text = name;
            t.AutoScroll = true;
            this.tabMain.TabPages.Add(t);
            return t;
        }

        public void OpenTab(string title, BaseUserControl form)
        {
            //check if exists
            var fType = form.GetType();
            for (int i = 0; i < this.tabMain.TabPages.Count; i++)
            {
                var t = this.tabMain.TabPages[i];
                if (t.Controls[0].GetType() == fType)
                {
                    this.tabMain.SelectedTabPage = t;
                    return;
                }
            }

            var tab = MakeNewTab(title);
            form.FormMain = this;
            form.Dock = DockStyle.Fill;
            tab.Controls.Add(form);
            tabMain.SelectedTabPage = tab;
        }


        private void ClearAllTabs()
        {
            this.tabMain.TabPages.Clear();

        }

        #endregion

        private void btnPhieuThuTien_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab("Phiếu Thu Tiền", new QuanLyVienPhi());
        }

        private void barButtonItem28_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmBangKeThuTien f = new frmBangKeThuTien(this.FormLogin.m);
            f.FormMain = this;
            f.ShowDialog(this);
        }

        private void btnDSBenhNhan_ItemClick(object sender, ItemClickEventArgs e)
        {
            ucDanhSachKham uc = new ucDanhSachKham(this.FormMain.m);
            OpenTab("Danh Sách Bệnh Nhân", uc);
        }

        private void btnTheKho_ItemClick(object sender, ItemClickEventArgs e)
        {
            //consider:
            frmThekho f = new frmThekho(m, "", s_mmyy, "d_thekho.rpt", "Thẻ kho");
            OpenTab("Thẻ Kho", f);
        }

        private void btnThuocVatTu_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmDmthuoc uc = new frmDmthuoc(this.FormLogin.m, i_userid, s_mmyy);
            OpenTab("Thuốc-Vật Tư", uc);
        }

        private void btnHoatChat_ItemClick(object sender, ItemClickEventArgs e)
        {
            
        }

        private void btnNhom_ItemClick(object sender, ItemClickEventArgs e)
        {
            //frmDmnhom uc = new frmDmnhom(
        }

        private void btnPhanMem_ItemClick(object sender, ItemClickEventArgs e)
        {
            //ucAbout ab = new ucAbout(
        }

    }
}